# coding:utf-8

import time
import thread

gen = None

def long_io():
    print("开始调用long_Io")
    def fun():
        print("start fun内部真实处理")
        time.sleep(2)
        print("end fun内部真实处理")
        global gen
        try:
            gen.send(1)
        except StopIteration:
            pass
    thread.start_new_thread(fun, ())
    print("long_Io函数返回")


def gen_coroutine(f):
    def wrapper(*args, **kwargs):
        global gen
        gen = f()
        gen.next()
    return wrapper

# @gen_coroutine
# def req_a()
# req_a = gen_coroutine(req_a)
@gen_coroutine
def req_a():
    print("start calling a")
    ret = yield long_io()
    print("ret %s" % ret)
    print("end a")


def req_b():
    print("start b")
    time.sleep(1)
    print("end b")


def main():
    """模拟主循环调度者"""
    # global  gen
    # gen = req_a()
    # gen.next()
    req_a()
    req_b()
    while 1:
        pass

if __name__ == "__main__":
    main()